# Who Will Defend Democracy? Evaluating Tradeoffs in Candidate Support Among Partisan Donors and Voters
# Step 11: Differences in candidate evaluation by party (Appendix, October 2018 sample)
# Last updated: June 19, 2020

# Initial settings --------------------------------------------------------

library(tidyverse)
library(cjoint)
library(ggthemes)

# Load relevant functions -------------------------------------------------

source("functions/specify_parameters.R")
source("functions/estimate_AMCEs.R")
source("functions/compare_AMCEs.R")
source("functions/order_attributes_levels.R")
source("functions/set_my_ggtheme.R")
source("functions/plot_AMCEs.R")
source("functions/specify_color_palette.R")
source("functions/plot_AMCEs_comparison.R")
source("functions/compare_AMCEs_no_N_labels.R")

# Load data ---------------------------------------------------------------

load("temp/data_for_analysis_public_october.RData")

# Specify parameters ------------------------------------------------------

parameters <- list(list(), list(), list())

w_race <- c(0.6, 0.2, 0.2)

# Selection of baselines

parameters <- specify_parameters(df.october, "Gender", "Male", 1)
parameters <- specify_parameters(df.october, "Race", "White", 0, w_race)
parameters <- specify_parameters(df.october, "Discrimination", "Not a big problem", 1)
parameters <- specify_parameters(df.october, "Taxes", "Less progressive", 1) 
parameters <- specify_parameters(df.october, "Courts", "Obey courts", 1)
parameters <- specify_parameters(df.october, "Investigations", "Independent", 1)
parameters <- specify_parameters(df.october, "Compromise", "Supports compromise", 1) 
parameters <- specify_parameters(df.october, "Voting", "Opposes voter ID laws", 1)

constraint_list <- list()

# Create model 

model <- formula(selected ~ 
                   `Gender` +
                   `Race` +
                   `Discrimination` +
                   `Taxes` +
                   `Courts` +
                   `Investigations` +
                   `Compromise` +
                   `Voting`)

# Set the order of attributes and levels ----------------------------------

Attribute_order <- c("Gender",
                     "Race",
                     "Discrimination",
                     "Taxes",
                     "Courts",
                     "Investigations",
                     "Compromise",
                     "Voting")

Level_order <- c(c("Male",
                   "Female"),
                 c("White",
                   "Black",
                   "Hispanic"),
                 c("Not a big problem",
                   "Prevent discrimination"),
                 c("Less progressive",
                   "More progressive"),
                 c("Obey courts",
                   "Disregard politicized decisions"),
                 c("Independent", 
                   "Partisan involvement"),
                 c("Supports compromise", 
                   "Stand up to other party"),
                 c("Opposes voter ID laws", 
                   "Supports voter ID laws"))

# Estimate ACIEs ----------------------------------------------------------

# Choose data for analysis

df.republican.public <- df.october %>% 
  filter(Partisanship == "Republican") %>% 
  select(-Partisanship) %>% 
  filter(!is.na(selected))

df.democrat.public <- df.october %>% 
  filter(Partisanship == "Democrat") %>% 
  select(-Partisanship) %>% 
  filter(!is.na(selected))

default_colour_palette <- estimate_AMCEs("ACIEs (Public, October 2018)", df.republican.public, model) %>%
  specify_color_palette(length(Attribute_order))

# Republicans evaluating Democrat vs. Republican candidates (Public)

rep.rep.public <- df.republican.public %>% filter(partisanship_dum_7 == 1) 
rep.dem.public <- df.democrat.public %>% filter(partisanship_dum_7 == 1)

rep.rep.public <- estimate_AMCEs("Republicans evaluating Republicans (Public, October 2018)", rep.rep.public, model)
rep.dem.public <- estimate_AMCEs("Republicans evaluating Democrats (Public, October 2018)", rep.dem.public, model)

comparison3 <- compare_AMCEs_2(rep.rep.public, rep.dem.public, "Republican candidates", "Democratic candidates")
plot_AMCEs_comparison("Republicans evaluate Republicans vs. Democrats (Public, October 2018)", comparison3)

# Democrats evaluating Democrat vs. Republican candidates (Public)

dem.rep.public <- df.republican.public %>% filter(partisanship_dum_7 == 2) 
dem.dem.public <- df.democrat.public %>% filter(partisanship_dum_7 == 2)

dem.rep.public <- estimate_AMCEs("Democrats evaluating Republicans (Public, October 2018)", dem.rep.public, model)
dem.dem.public <- estimate_AMCEs("Democrats evaluating Democrats (Public, October 2018)", dem.dem.public, model)

comparison4 <- compare_AMCEs_2(dem.rep.public, dem.dem.public, "Republican candidates", "Democratic candidates")
plot_AMCEs_comparison("Democrats evaluate Republicans vs. Democrats (Public, October 2018)", comparison4)

